package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.adapters.old.TextViewAdapter
import com.hinnotek.amoeba.bean.old.AmibaInstallByIdBean
import com.hinnotek.amoeba.databinding.ActivityTiOtherAmoebaInstalledDetailBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.AmibaInstallViewModel
import com.lee.common.network.net.ObserverState
import dagger.hilt.android.AndroidEntryPoint
import java.util.*

@AndroidEntryPoint
class TIOtherAmoebaInstalledDetailActivity : BaseActivity<ActivityTiOtherAmoebaInstalledDetailBinding>() {
    private var adapter: TextViewAdapter? = null
    val viewModel by viewModels<AmibaInstallViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setLeftVisible(true)
        setTitle("其他阿米巴安装申请")

        setContentView(R.layout.activity_ti_other_amoeba_installed_detail)
    }

    override fun initVew() {
        val manager = LinearLayoutManager(mContext)
        manager.orientation = RecyclerView.VERTICAL

        bindingView.insView.rlvTiContinuedPriceIncrease.layoutManager = manager
        bindingView.insView.rlvTiContinuedPriceIncrease.adapter = TextViewAdapter(R.layout.item_text_view).also {
            adapter = it
        }
    }

    override fun setData() {
        val amibaInstallId = intent.getIntExtra("amibaInstallId", 0)
        getAmibaInstallById(amibaInstallId)
    }

    /**
     * app端通过id获取单个安装申请接口
     */
    fun getAmibaInstallById(amibaInstallId: Int) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()

        viewModel.getAmibaInstallById(amibaInstallId).observe(this, object :
            ObserverState<AmibaInstallByIdBean>() {
            override fun onDataChange(data: AmibaInstallByIdBean) {

                bindingView.insView.tvTiChooseAmoeba.text = data.orgName
                bindingView.insView.tvTiUnit.text = data.customerName
                bindingView.insView.tvTiRelatedProject.text = data.projectName
                bindingView.insView.tvTiInstallDate.text = data.installDate
                bindingView.insView.tvTiEndDate.text = data.endDate
                bindingView.insView.tvTiInstallLocation.text = data.installPosition
                bindingView.insView.tvTiPreQuote.text = data.preMoney
                bindingView.insView.tvTiApplicant.text = data.createUser
                bindingView.insView.tvTiApprover.text = data.approvalUser

                val statusView = bindingView.insView.tvTiApplicantStatus

                if (data.approvalState == 1) {
                    statusView.text = "已申请"
                    statusView.setTextColor(getColor21(R.color.black))
                } else if (data.approvalState == 2) {
                    statusView.text = "已同意"
                    statusView.setTextColor(getColor21(R.color.xui_btn_green_normal_color))
                } else if (data.approvalState == 3) {
                    statusView.text = "已拒绝"
                    statusView.setTextColor(getColor21(R.color.xui_config_color_red))
                }
                bindingView.insView.tvTiCc.text = data.sendUser
                val strings: MutableList<String> = ArrayList()
                for (amibaInstallContinueds in data.amibaInstallContinueds) {
                    strings.add(amibaInstallContinueds.continuedMoney)
                }
                adapter?.setList(strings)

                super.onDataChange(data)
            }

            override fun onError(msg: String?, code: Int) {
                customDialog.dismiss()
                ToastUtils.showShort(mContext, msg)
                super.onError(msg, code)
            }
        })

    }
}